package io.api.modules.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.admin.common.page.PageData;
import io.admin.common.service.impl.CrudServiceImpl;
import cn.hutool.core.util.StrUtil;
import io.admin.common.utils.ConvertUtils;
import io.api.modules.project.dao.LineDao;
import io.api.modules.project.dto.LineDTO;
import io.api.modules.project.dto.UserLineDTO;
import io.api.modules.project.entity.LineEntity;
import io.api.modules.project.service.LineService;
import org.springframework.stereotype.Service;

import javax.sound.sampled.Line;
import java.util.*;

/**
 * 项目线路关系表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-07-12
 */
@Service
public class LineServiceImpl extends CrudServiceImpl<LineDao, LineEntity, LineDTO> implements LineService {

    @Override
    public QueryWrapper<LineEntity> getWrapper(Map<String, Object> params){
        Long projectId = (Long) params.get("projectId");
        String name = (String)params.get("name");

        QueryWrapper<LineEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(Objects.nonNull(projectId), "project_id", projectId);
        wrapper.like(StrUtil.isNotBlank(name), "name", name);
        return wrapper;
    }


    @Override
    public List<LineDTO> list(Long projectId) {
        Map<String, Object> params = new HashMap<>();
        params.put("projectId", projectId);
        return super.list(params);
    }

    @Override
    public int countByNameAndProjectId(String name, Long projectId, Long lineId) {
        return baseDao.countByNameAndProjectId(name, projectId, lineId);
    }

    @Override
    public PageData<UserLineDTO> pageLine(Map<String, Object> params) {
        IPage<LineEntity> page = getPage(params, "create_date", false);
        List<LineEntity> list = baseDao.getList(params);
        return getPageData(list, page.getTotal(), UserLineDTO.class);
    }

    @Override
    public List<LineDTO> selectList(Map<String, Object> params) {
        List<LineEntity> list = baseDao.getList(params);
        return ConvertUtils.sourceToTarget(list, LineDTO.class);
    }

}